Ext.define('AM.view.PostView', {
		extend: 'Ext.grid.Panel',
		alias : 'widget.postView',
		store : 'PostStore',
		height: 200,
		width: 400,
//		frame:true,
		columnLines:true,
//		selModel : Ext.create('Ext.selection.CheckboxModel'),
		selModel: {
	        injectCheckbox: 0,
	        mode: "SIMPLE"    //"SINGLE"/"SIMPLE"/"MULTI"
	       
   		 },
   		selType: "checkboxmodel",
    	dockedItems: [{
        xtype: 'pagingtoolbar',
        dock: 'bottom',
        store : 'PostStore',
        displayInfo: true
    }],
		columns: [	
			        {text: '序号', dataIndex: 'id',width:0,align:'center'},
			       {text: '序号',  xtype: 'rownumberer',width:50,align:'center',tdCls:'background-color: #8EE5EE;'},
			        { text: '名称', dataIndex: 'name', width:200,align:'center'},
			        { text: '科室', dataIndex: 'department.name', width:200,align:'center'}
//			        { text: '岗位系数', dataIndex: 'postcoefficient', width:100,align:'center'}
//			        { text: '时长', dataIndex: 'postHour', width:80,align:'center'}
				 ],
			   tbar: [
					  { xtype: 'button',id:'tab-6_add', text: '增加',handler: addPost,icon: 'Ext/restful/images/add.png'
					  },
					  { xtype: 'button', id:'tab-6_edit',text: '修改',handler: editPost,icon: 'Ext/restful/images/cog_edit.png'
					  },
					  { xtype: 'button',  id:'tab-6_delete',text: '删除',handler: deletePost,icon: 'Ext/restful/images/delete.png'
					  },
					  {
					     xtype : "combobox",
					     allowBlank:false,
					     labelAlign :"right",
						 fieldLabel: '选择科室',
						 id:"searchpost",
						 store :Ext.create('AM.store.TargetDepartStore'),
						 displayField: 'name',
						 valueField: 'id',
					     width:300
					  },
					  { xtype: 'button', text: '查询', id:'tab-6_search',handler: searchPost,icon : 'Ext/restful/images/find.png'
					  }
					]
		
})
function searchPost(){
	var grid=this.ownerCt.ownerCt;
			var depart= Ext.getCmp('searchpost').getValue();
			
		  grid.getStore().on('beforeload', function (store, options) {
          var new_params = {depart:depart}
                Ext.apply(store.proxy.extraParams, new_params);
       });//此处为加载条件
		  grid.getStore().reload();
					
}

 function addPost() {
		 var myform1= new Ext.form.Panel({
			        bodyPadding: 5,
			        labelWidth : 65,
					   items: [{   
					   		      allowBlank:false,
					   			  xtype : "textfield",
					   		      fieldLabel: '岗位名称',
					   			  labelAlign :"right",
							      name: 'name',
							      width:300
							},{
								 xtype : "combobox",
							     allowBlank:false,
							     labelAlign :"right",
								 fieldLabel: '所属科室',
								 name: 'department.id',
								 store :"TargetDepartStore",
								 displayField: 'name',
    							 valueField: 'id',
							     width:300
							}
//							,{
//								 xtype : "numberfield",
//							     labelAlign :"right",
//							     fieldLabel: '岗位系数',
//							     step :0.01,
//							     name: 'postcoefficient',
//							     blankText:"必须为数字",
//							     minValue: 0,
//							     width:300
//							},{
//								 xtype : "numberfield",
//								 nanText:"请输入0到1之间的小数",
//							     labelAlign :"right",
//							     fieldLabel: '岗位时长',
//							     name: 'postHour',
//							     blankText:"必须为数字",
//							     minValue: 0,
//							     maxValue:12 ,
//							     minText:"最小值为0",
//							     maxText:"最大值为12",
//							     width:300
//							}
							]		        	
				        	       

		 })
		    var grid=this.ownerCt.ownerCt;
			var win = new Ext.Window({
				title : "添加岗位",
				width : 500,
				height : 200,
				layout : "fit",
				frame:true,
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'post_addPost',
											 success : function() {
												Ext.Msg.alert('提示', '添加成功');
												win.close();
												grid.getStore().reload();
											},
											failure : function() {
												Ext.Msg.alert('错误', '添加失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
								win.close();
											
							}
						}]
			});
		win.show();
	}



   function editPost() {
   		 var grid=this.ownerCt.ownerCt;
		 var myform1= new Ext.form.Panel({
			        bodyPadding: 5,
			        labelWidth : 65,
			        items: [{   
					   			  xtype : "textfield",
					   			  allowBlank:false,
					   		      fieldLabel: '岗位名称',
					   			  labelAlign :"right",
							      name: 'name',
							      width:300
							      
							},{
								  xtype : "textfield",
					   		      fieldLabel: '职位id',
					   			  labelAlign :"right",
							      name: 'id',
							      hidden : true 
							},{
								 xtype : "combobox",
							     allowBlank:false,
							     labelAlign :"right",
								 fieldLabel: '所属科室',
								 name: 'department.id',
								 store :"TargetDepartStore",
								 displayField: 'name',
    							 valueField: 'id',
    							 width:300
							     
							}
//							,{
//								 xtype : "numberfield",
//							     labelAlign :"right",
//							     fieldLabel: '岗位系数',
//							     step :0.01,
//							     name: 'postcoefficient',
//							     blankText:"必须为数字",
//							     minValue: 0,
//							     width:300
//							     
//							},{
//								 xtype : "numberfield",
//								 nanText:"请输入0到1之间的小数",
//							     labelAlign :"right",
//							     fieldLabel: '岗位时长',
//							     name: 'postHour',
//							     blankText:"必须为数字",
//							     minValue: 0,
//							     maxValue:12 ,
//							     minText:"最小值为0",
//							     maxText:"最大值为12",
//							     width:300
//							}
							]		        	
		
		 })
		  var win = new Ext.Window({
				title : "修改岗位",
				width : 500,
				height : 200,
				layout:'fit',
				frame:true,
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								
								win.getComponent(0).getForm().submit({
											 url : 'post_editPost',
											 success : function() {
												Ext.Msg.alert('提示', '修改成功');
												win.close();
												grid.getStore().reload();
												grid.getSelectionModel().deselectAll();
											},
											failure : function() {
												Ext.Msg.alert('错误', '修改失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
									win.close();
									grid.getSelectionModel().deselectAll();
											
							}
						}]
			});
//		var grid=this.ownerCt.ownerCt;
		var selections = grid.getSelectionModel().getSelection();
						if(selections.length==0){
							Ext.MessageBox.alert("提示","请选择要编辑的一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							var record = selections[0];
						//这里是修改操作的实现部分
							win.getComponent(0).getForm().loadRecord(record);
							win.show();
						}
	}

	function deletePost(){
				 var grid=this.ownerCt.ownerCt;
			     var selections = grid.getSelectionModel().getSelection();
					if(selections){
					if(selections.length==0){
							Ext.MessageBox.alert("提示","请检查是否选中某一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							Ext.MessageBox.confirm("提示", "确认要删除 ？", function(btn) {
							if (btn == 'yes') {			
							var record = selections[0];
								Ext.Ajax.request({
										url : 'post_deletePost',
										success : function() {
											Ext.MessageBox.alert("提示",
													"删除" + record.get("name")+ "成功！");
												grid.remove(selections );
												grid.getStore().reload();
										},
										failure : function() {
											Ext.MessageBox.alert("提示", "岗位呗引用，删除失败！");
										},
										params : {
											id : record.get("id")
										}
									});			
								}
					  	})
                   	} 
 			   
		   }				
	}
	 	
